import {Point} from '../../type';
import {PlotTypes} from '../../enum/PlotTypes';
import * as PlotUtils from '../../utils';

// @ts-ignore
export default class Marker extends ol.geom.Point {
    public params: any;
    public isPlot: boolean = true;
    public fixPointCount: number = 1;

    protected points: Array<Point>;

    protected _type: PlotTypes;

    constructor(coordinates: any, points: Array<Point>, params: any) {
        super([]);
        this._type = PlotTypes.POINT;
        this.points = [];
        this.params = params;
        if (points && points.length > 0) {
            this.setPoints(points);
        } else if (coordinates && coordinates.length > 0) {
            // @ts-ignore
            this.setCoordinates(coordinates);
        }
    }

    public transform(type: string, points: Array<Array<number>>): void {
        const [topLeft, _, bottomRight] = points;
        if (type === 'scale') {
            this.updatePoint(topLeft as Point, 0);
            this.updatePoint(bottomRight as Point, 1);
        }
    }

    /**
     * 获取标绘类型
     * @return {PlotTypes}
     */
    public getPlotType(): PlotTypes {
        return this._type;
    }

    /**
     * 获取坐标点
     * @return {Array<Point>}
     */
    public getPoints(): Array<Point> {
        return this.points.slice(0);
    }

    /**
     * 获取点数理
     * @return {number}
     */
    public getPointCount(): number {
        return this.points.length;
    }

    /**
     * 更新当前坐标
     * @param point
     * @param index
     */
    public updatePoint(point: Point, index: number): void {
        if (index >= 0 && index < this.points.length) {
            this.points[index] = point;
            this.generate();
        }
    }

    /**
     * 完成绘制
     */
    public finishDrawing(): void {

    }

    private setPoints(value: Array<Point>): void {
        this.points = !value ? [] : value;
        if (this.points.length >= 1) {
            this.generate();
        }
    }

    private generate(): void {
        // @ts-ignore
        this.setCoordinates(this.points[0]);
    }

    private generatePoints(center: Point, radius: number): Array<Point> {
        let [x, y, angle] = [0, 0, 0];
        const points: Array<Point> = [];
        for (let i: number = 0; i <= 100; i++) {
            angle = Math.PI * 2 * i / 100;
            x = center[0] + radius * Math.cos(angle);
            y = center[1] + radius * Math.sin(angle);
            points.push([x, y]);
        }
        return points;
    }
}